<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreSettingBase extends Base {
	
	//constructor
	function CoreSettingBase() {
		parent::Base();
		
		$this->owner = 0;
	}
	
	//methods
	function getSettings() {
		$settings = $this->_default_settings();
		$key = $this->_key($key);
		$query = "select settings_key, settings_value from pp_settings where " . $this->_query_owner('settings_owner') . " ";
		
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			if( substr($row['settings_key'], 0, strlen('setting_')) == 'setting_' ) {
				$key = substr($row['settings_key'], strlen('setting_'));
			} else {
				$key = $row['settings_key'];
			}
			$settings[$key] = $row['settings_value'];
		}
		
		return $settings;
	}
	
	function append() {
		global $SETTINGS;
		
		$a = $this->getSettings();
		if( is_array($a) ) {
			foreach($a as $k => $v) {
				$SETTINGS[$k] = $v;
			}
		}
	}
	
	function getItem($key) {
		$key = $this->_key($key);
		$query = "select settings_key, settings_value from pp_settings where " . $this->_query_owner('settings_owner') 
				. " and settings_key = '" . $key . "'";
		
		$row = $this->adb->getRow($query);
		$item = array();
		if( is_array($row) && $row['settings_key'] ) {
			$item['key'] = $row['settings_key'];
			$item['value'] = $row['settings_value'];
		}
		
		return $item;
	}
	
	function getItemValue($key) {
		$key = $this->_key($key);
		$query = "select settings_key, settings_value from pp_settings where " . $this->_query_owner('settings_owner') 
				. " and settings_key = '" . $key . "'";
		
		$row = $this->adb->getRow($query);
		if( is_array($row) && $row['settings_value'] ) {
			return $row['settings_value'];
		}
		
		return '';
	}
	
	function getItemB($key) {
		$item = $this->getItem($key);
		if( $item['key'] ) {
			//
		} else {
			$this->addItem($key, '');
			$item = array();
			$item = array('key' => $key, 'value' => '');
		}
		
		return $item;
	}
	
	function addItem($key, $value) {
		$key = $this->_key($key);
		$value = func_db_input($value);
		
		if( $value == 'now()' ) {
			$query = "insert into pp_settings set settings_owner = '" . $this->owner . "', settings_key = '" . $key . "', settings_value = now() ";
		} elseif( $value == 'add_once()' || $value == '++' ) {
			$query = "insert into pp_settings set settings_owner = '" . $this->owner . "', settings_key = '" . $key . "', settings_value = settings_value + 1 ";
		} elseif( $value == '--' ) {
			$query = "insert into pp_settings set settings_owner = '" . $this->owner . "', settings_key = '" . $key . "', settings_value = settings_value - 1 ";
		} else {
			$query = "insert into pp_settings set settings_owner = '" . $this->owner . "', settings_key = '" . $key . "', settings_value = '" . $value . "' ";
		}
		
		return $this->adb->query($query);
	}
	
	function addItemB($key, $value) {
		$item = $this->getItem($key);
		if( $item['key'] ) {
			$this->updateItem($key, $value);
		} else {
			$this->addItem($key, $value);
		}
	}
	
	function updateItem($key, $value) {
		$key = $this->_key($key);
		$value = func_db_input($value);
		
		if( $value == 'now()' ) {
			$query = "update pp_settings set settings_value = now() where " . $this->_query_owner('settings_owner') 
					. " and settings_key = '" . $key . "'";
		} elseif( $value == 'add_once()' || $value == '++' ) {
			$query = "update pp_settings set settings_value = settings_value + 1 where " . $this->_query_owner('settings_owner') 
					. " and settings_key = '" . $key . "'";
		} elseif( $value == '--' ) {
			$query = "update pp_settings set settings_value = settings_value - 1 where " . $this->_query_owner('settings_owner') 
					. " and settings_key = '" . $key . "'";
		} else {
			
			$query = "update pp_settings set settings_value = '" . $value . "' where " . $this->_query_owner('settings_owner') 
					. " and settings_key = '" . $key . "'";
		}
		
		
		return $this->adb->query($query);
	}
	
	function updateItemB($key, $value) {
		$item = $this->getItem($key);
		if( strlen($item['key']) > 0 ) {
			$this->updateItem($key, $value);
		} else {
			$this->addItem($key, $value);
		}
	}
	
	function deleteItem($key) {
		$key = $this->_key($key);
		
		$query = "delete from pp_settings where " . $this->_query_owner('settings_owner') . " and settings_key = '" . $key . "'";
		
		return $this->adb->query($query);
	}
	
	//private
	function _key($key) {
		return 'setting_' . $key;
	}
	
	function _default_settings() {
		$default_settings = array(
				//email
				'EMAIL_CHARSET' => '',
				//
				'bs_countryname' => 'United States',
				'bs_countryid' => '223',
				//
				'shopping_countryname' => 'United States',
				'shopping_countryid' => '223',
				'shopping_zoneid' => '12',
				'shopping_zonename' => 'CA',
				'shopping_zonecode' => 'CA',
					);
		
		return $default_settings;
	}
}
//
?>